﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using plas.core.support;
using plas.cps.api.entity;
using plas.core.Tools;
using plas.core.entity;
using plas.core.exception;
using System.Linq.Expressions;
using plas.cps.api.services;
namespace plas.cps.impl.service
{
    public class GoodsServicesImpl : AbstractBaseServiceSupport<GoodsEntity>, IGoodsServices
    {
        /// <summary>
        /// 保存物品实体，并且验证数据
        /// </summary>
        /// <param name="good"></param>
        public void Save(params GoodsEntity[] goods)
        {
            foreach (GoodsEntity good in goods)
            {
                CheckGoodsModel(good);
                if (good.Operate == BaseEntity.OperateType.OPERATE_INSERT)
                {
                        good.GoodsPid = UUIDMaker.makUUID();
                        this.dao.insert(good);
                        good.Operate = BaseEntity.OperateType.OPERATE_UPDATE;
                  
                }
                else if (good.Operate == BaseEntity.OperateType.OPERATE_UPDATE)
                {
                    if (string.IsNullOrEmpty(good.GoodsPid))
                    {
                        throw new Exception("物品编号不可为空");
                        
                    }
                    this.dao.update(good);                                         
                }
            }

        }
        private void CheckGoodsModel(GoodsEntity good)
        {
            if (good == null)
            {
                throw new Exception("物品信息不可为空");
            }
            if (string.IsNullOrEmpty(good.GoodsName))
            {
                throw new Exception("物品名称不可为空");
            }
            if (string.IsNullOrEmpty(good.GoodsType))
            {
                throw new Exception("物品类型不可为空");
            }
            if (double.IsNaN(good.GoodsPrice))
            {
                throw new Exception("物品单价不可为空");
            }
            if (string.IsNullOrEmpty(good.GoodsImage))
            {
                throw new Exception("图片地址不可为空");
            }
        }
        /// <summary>
        /// 删除物品信息
        /// </summary>
        /// <param name="pk">物品主键数组</param>
        public void Delete(params string[] pks)
        {           
            foreach(string pid in pks)
            {
                if (string.IsNullOrEmpty(pid))
                {
                    throw new Exception("请选择需要删除的物品");
                }
                this.dao.delete(pid);
            }
        }
        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="pk">物品资源主键</param>
        public GoodsEntity QueryGoodById(string pk)
        {
            if (string.IsNullOrEmpty(pk))
            {
                throw new Exception("请提供查询物品的id");
            }
            return this.dao.queryById(pk);

        }
        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="pageNum">当前页</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="goods">物品资源实体</param>
        /// <param name="orderBy">键值对, 可以不赋值，参见实例 </param>
        /// <returns></returns>
        public QueryResult<GoodsEntity> QueryList(int pageNum, int pageSize, GoodsEntity goods, Dictionary<string, string> orderBy)
        {
            if (goods != null)
            {
                Expression<Func<GoodsEntity, bool>> body = PredicateExtensionsUtil.True<GoodsEntity>();
                if (!string.IsNullOrEmpty(goods.GoodsName))//通过物品名称查询
                    body = body.And(o => o.GoodsName.Contains(goods.GoodsName));
                return this.dao.queryList(pageNum, pageSize, null, orderBy);
            }
            else
            {
                return this.dao.queryList(pageNum, pageSize, null, orderBy);
            }
        }

    }
}
